<?php

include_once "../../models/core.php";

// 获取页面上提交的数据
$username = $_POST["username"];
$password = $_POST["password"];
$age = $_POST["age"];
$gender = $_POST["gender"];

// 用户注册，插入新的数据到数据库
register($conn, $username, $password, $age, $gender);

// 用户注册，插入新的数据到数据库
function register($conn, $username, $password, $age, $gender)
{
    // mysqli_stmt
    // 插入数据库                       
    $sql = "INSERT INTO users(username, password, age, gender) VALUES(?, ?, ?, ?)";
    $mysqli_stmt = $conn->prepare($sql); // 准备预处理语句

    // s: string    i: int
    $mysqli_stmt->bind_param('ssii', $username, $password, $age, $gender);

    // 执行预处理的语句
    if($mysqli_stmt->execute()){
        // echo $mysqli_stmt->insert_id; // 程序执行成功，返回插入数据表的行 id
        echo PHP_EOL;
        echo "<script>alert('注册成功!!!'); window.location.href='../../views/office/login.html'</script>";
    }else{
        echo $mysqli_stmt->error; // 执行失败，展示错误信息

        // 注册失败的话，继续跳转到 register.html 页面
        echo "<script>alert('注册失败!!!'); window.location.href='../../views/office/register.html'</script>";
    }

    $mysqli_stmt->free_result();
    $mysqli_stmt->close();
}



// 释放结果集
$conn->close(); 
